import allure
from airtest.core.api import *

from common.airtest.airtest_base import AirtestBase, exists_1
from common.log.log import log
from config.conf import BASE_DIR
from po.jinhua.applets_po.good.good_page import GoodPage
from po.jinhua.applets_po.home.search_page import SearchPage
from po.jinhua.applets_po.my.my_page import MyPage
from po.jinhua.applets_po.product_details_page import ProductDetailsPage
from po.jinhua.applets_po.shopping_cart.shopping_cart_page import ShoppingCartPage


class HomePage:
    """
    小程序首页
    """

    def __init__(self):
        element_1 = BASE_DIR + r"/element_picture/首页.png"
        element_2 = BASE_DIR + r"/element_picture/首页_灰色.png"
        element_3 = BASE_DIR + r"/element_picture/X.png"
        self.drive = AirtestBase('花王云购')
        if exists(Template(element_1)):
            self.drive.click_5(element_1)
        elif exists(Template(element_2)):
            self.drive.click_5(element_2)

    @allure.step('小程序底部点击进入：我的')
    def go_to_my(self):
        """
        进入：我的
        :return:
        """
        try:
            log.info('小程序底部点击进入：我的')
            self.drive.click_5(BASE_DIR + r"/element_picture/我的.png")
            if exists_1(BASE_DIR + r"/element_picture/我的/顶部-我的.png"):
                log.info('已经在顶部了')
                return MyPage()
            else:
                AirtestBase().up_or_down('up')
                if exists_1(BASE_DIR + r"/element_picture/我的/顶部-我的.png"):
                    log.info('已滑动到顶部')
                    return MyPage()
                else:
                    log.info('进入小程序【我的】出现异常')
                    raise Exception
        except Exception as e:
            log.error(f'进入小程序【我的】出现异常为：{e}')
            raise e

    @allure.step('循环查找元素图片，找到后根据mode进行后续操作')
    def loop_search_element(self, element, num=2, mode=1):
        """
        循环查找元素图片，找到后根据mode进行后续操作
        num=2 ，默认向下按15*2次
        mode=1: 找到后点击（默认）
        mode=2：找到后返回True
        """
        try:
            self.drive.up_down_search_element_and_click(element, num, mode)
            return ProductDetailsPage()
        except Exception as e:
            log.error(f'循环查找元素并打开商品出现异常为：{e}')
            raise e

    @allure.step('进入搜索页')
    def open_search_page(self):
        try:
            self.drive.click_5(BASE_DIR + r"/element_picture/首页/搜索按钮.png")
            return SearchPage()
        except Exception as e:
            log.error(f'进入搜索页出现异常为：{e}')
            self.drive.click_5(BASE_DIR + r"/element_picture/首页/搜索按钮_win7.png")
            # raise e

    @allure.step('点击大品类')
    def click_big_goods(self, good_ele):
        try:
            log.info('点击大品类')
            self.drive.click_5(BASE_DIR + fr"/element_picture/首页/{good_ele}.png")
            return GoodPage()
        except Exception as e:
            log.error(f'首页，点击大品类出现异常为：{e}')
            raise e

    @allure.step('小程序首页，打开任一商品，进入商品详情页')
    def open_any_product_details(self, mode):
        """
        小程序首页，打开任一商品，进入商品详情页
        """
        try:
            if mode == 0:
                log.info('通过向下滑动查找商品右侧VIP图片，点击进入商品详情页')
                element_1 = BASE_DIR + r"/element_picture/首页/首页商品价格右侧VIP图标.png"
                AirtestBase().up_down_search_element_and_click(element_1, num=5)
                return ProductDetailsPage()
            elif mode == 1:
                log.info('通过搜索“自动化”在结果页点击购物车按钮进入商品详情页')
                element_2 = BASE_DIR + r"/element_picture/搜索结果页/搜索结果页购买按钮.png"
                self.open_search_page().search_keyword('自动化')
                AirtestBase().click_5(element_2)
                return ProductDetailsPage()
        except Exception as e:
            log.error(f'小程序首页，打开任一商品，进入商品详情页出现异常为：{e}')
            raise e

    @allure.step('进入分类，商品分类列表')
    def go_to_classification(self, classification):
        """
        进入分类
        """
        try:
            log.info('进入分类，商品分类列表')
            self.drive.click_5(BASE_DIR + r"/element_picture/分类/分类-高亮.png")
            self.drive.click_5(BASE_DIR + fr"/element_picture/分类/{classification}.png")
            sleep(2)  # 等待系统加载
        except Exception as e:
            log.error(f'进入分类，商品分类列表出现异常为：{e}')
            raise e

    @allure.step('小程序，进入购物车')
    def go_to_shopping_cart(self):
        """
        小程序，进入购物车
        """
        try:
            log.info('小程序，进入购物车')
            self.drive.click_5(BASE_DIR + r"/element_picture/购物车.png")
            return ShoppingCartPage()
        except Exception as e:
            log.error(f'进入分类，商品分类列表出现异常为：{e}')
            raise e
